home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
ddj0897.zip
/
JAVAQ&A.ASC
< prev
next >
Wrap
Text File
|
1997-06-20
|
2KB
|
90 lines
_Java Q&A_
by Cliff Berg
// DirectoryLister.java
public class DirectoryLister extends java.applet.Applet
implements java.awt.event.ActionListener
{
private java.awt.TextField tf;
private java.awt.TextArea ta;
private java.awt.Button b;
String[] files;
public static void main(String[] args)
{
java.awt.Frame f = new java.awt.Frame();
f.setSize(400, 220);
f.show();
DirectoryLister dl = new DirectoryLister();
f.add("Center", dl);
dl.init();
dl.start();
}
public void init()
{
setLayout(null);
ta = new java.awt.TextArea();
add(ta);
ta.setSize(300, 100);
ta.setLocation(10, 10);
tf = new java.awt.TextField();
add(tf);
tf.setSize(300, 20);
tf.setLocation(10, 124);
b = new java.awt.Button("Display");
add(b);
b.setSize(100, 20);
b.setLocation(10, 154);
b.addActionListener(this);
// Attempt to get the current directory
String dir = System.getProperty("user.dir");
getListing(dir);
displayListing();
}
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object source = event.getSource();
if (source == b)
{
System.out.println("button clicked");
// Obtain listing for the specified directory
getListing(tf.getText());
// Display the listing in the text window
displayListing();
}
}
protected void getListing(String dir)
{
java.io.File f = new java.io.File(dir);
files = f.list();
}
protected void displayListing()
{
ta.setText("");
for (int i = 0; i < files.length; i++)
{
ta.append(files[i]);
ta.append("\r\n");
}
}
}